function love.load()
	setGameTitle("太空大胡子 - aka 2018.06.09")
	setMouse("/gameImage/zhunxin.png",9,9)
	setBackgroudImg("/backgroudImage/b2.jpg")
	setPlayImg("/gameImage/play.png")
	setEnemysImg("/gameImage/diren.png")
	setEnemysBulletsImg("/gameImage/direnzidan.png")
	setDefaultPosition(395,540)
	setPlayerData(395,540,1,1)
	setBulletsState()
	setPlayerBulletsPhoto("/gameImage/zidan.png",10,10)
	enemyState()
	setEnemyNumber(5,30,120)
	enemysBulletsState()
	enemysBulletsSpeed(170)
	setBGM("/sound/backgroud.ogg")
end

function love.update(dt)
	if player then
		keyboardMove(dt,300)
		setBulletsFire(dt)
		mouseControlBulletsMove()
		setEnemysBullets(dt)
	end
end

function love.draw()
	if player then
		getBackgroudImg()
		getPlayerSkeleton("line",player.x, player.y, player.w, player.h)
		drawBulletsPhoto()
		setPlayerBullets()
		getPlayerImgAndDefaultPosition()
		drawEnemySkeleton()
		drawEnemysBullets()
		drawEnemysImg()
		drawEnemysBulletsImg()
	else
		gameOver("Game Over !")
	end

	if table.maxn(enemys) == 0 then
		gameOver("You Win !")
	end
end

function love.mousepressed(x,y,button,istouch)
	if button == 1 then
		leftPressed = true
	end
end

function love.mousereleased(x,y,button,istouch)
	if button == 1 then
		leftPressed = nil
	end
end

-- 游戏结束
function gameOver(gameOverTitle)
	love.graphics.print(gameOverTitle, 370, 300)
end

-- 设置敌机子弹状态
function enemysBulletsState()
	enemysBullets = {}
end

-- 设置敌机子弹速度
function enemysBulletsSpeed(speed)
	enemysBulletsSpeed = speed
end

-- 设置敌机子弹以及子弹运动
function setEnemysBullets(dt)
	esx = player.x+player.w/2
	esy = player.y+player.h/2
	for _,v in ipairs(enemys) do
		if v.fire_int == 0 then 
			v.fire_int = love.math.random(30,60)
			ebx = v.x + v.w/2
			eby = v.y + v.h/2
			edx = esx-ebx
			edy = esy-eby
			el = math.sqrt((edx*edx) + (edy*edy))
			ezx = enemysBulletsSpeed * (edx)/el
			ezy = enemysBulletsSpeed * (edy)/el
			table.insert(enemysBullets,{x=(v.x + v.w/2), y=(v.y + v.h/2), w=1, h=1, zx=ezx, zy=ezy})
			setEnemysFireSWF("/sound/direnkaihuo.mp3")
		else
			v.fire_int = v.fire_int - 1
		end
	end

	for z=#enemysBullets,1,-1 do
		q = enemysBullets[z]
		q.x = q.x + q.zx*dt
		q.y = q.y + q.zy*dt
		local windowWidth = getWindowWidth()
		local windowHeight = getWindowHeight()
		if q.x > windowWidth or q.x < 0
		or q.y > windowHeight or q.y < 0 then
			table.remove(enemysBullets,z)
		else
			if collision(q,player) then
				player = nile
				setPlayDieSWF("/sound/nvdie.mp3")
			end
		end
	end
end

-- 绘制敌机子弹
function drawEnemysBullets()
	for _,v in ipairs(enemysBullets) do
		love.graphics.rectangle("line",v.x, v.y, v.w, v.h)
	end
end

-- 设置敌机状态
function enemyState()
	enemys = {}
end

-- 设置敌机数量和属性
function setEnemyNumber(column,fireFirst,fireEnd)
	for i=1,11 do
		for j=1,column do
			table.insert(enemys,{x=30+(i-1)*70, y=30+(j-1)*70,w=40,h=40,hp=5,fire_int=love.math.random(fireFirst,fireEnd)})
		end
	end
end

-- 绘制敌机骨架
function drawEnemySkeleton()
	for _,v in ipairs(enemys) do
		love.graphics.setColor(255, 255-(5-v.hp)*60, 255-(5-v.hp)*60, 1)
		love.graphics.rectangle("line",v.x, v.y, v.w, v.h)
	end
end

-- 键盘控制角色移动
function keyboardMove(dt,px)
	if love.keyboard.isDown('w') or  love.keyboard.isDown('up') then
		vy = vy - px*dt 
		player.y = player.y - px*dt 
	elseif love.keyboard.isDown('s') or  love.keyboard.isDown('down') then 
		vy = vy + px*dt 
		player.y = player.y + px*dt 
	end

	if love.keyboard.isDown('a') or love.keyboard.isDown('left') then
		vx = vx - px*dt 
		player.x = player.x - px*dt 
	elseif love.keyboard.isDown('d') or love.keyboard.isDown('right') then 
		vx = vx + px*dt 
		player.x = player.x + px*dt 
	end

	local windowW = getWindowWidth()
	local windowH = getWindowHeight()

	if player.x < 0 then player.x = 0 end
	if vx < 0 then vx = 0 end
	if vx > windowW then vx = windowW end
	if player.y < 0 then player.y = 0 end
	if vy < 0 then vy = 0 end
	if vy > windowH then vy = windowH end
	player.x = math.min(player.x,windowW-player.w)
	vx = math.min(vx,windowW+vx)
	player.y = math.min(player.y,windowH-player.h)
	vy = math.min(vy,windowW+vy)
end

-- 判断角色碰撞
function collision(a,b)
	if player then
		if a.x + a.w < b.x then return false end
		if a.x > b.x + b.w then return false end
		if a.y + a.h < b.y then return false end
		if a.y > b.y + b.h then return false end
		return true
	end
end

-- 设置鼠标光标
function setMouse(mouseImg,mx,my)
	cursor = love.mouse.newCursor(mouseImg, mx, my)
	love.mouse.setCursor(cursor)
end

-- 鼠标控制子弹移动
-- 设置子弹发射间隔
shoot_delay = 3
function mouseControlBulletsMove()
	-- 设置子弹飞行速速度
	bullet_speed = 400
	if leftPressed or love.keyboard.isDown('j') then
		if shoot_delay>0 then
			shoot_delay = shoot_delay-1
		else
			shoot_delay = 3
			if leftPressed then
				mouseX = love.mouse.getX()
				mouseY = love.mouse.getY()
				l = math.sqrt((mouseX - player.x)*(mouseX - player.x) + (mouseY - player.y)*(mouseY - player.y))
				px = bullet_speed * ((mouseX - player.x)/l)
				py = bullet_speed * ((mouseY - player.y)/l)
			else
				px = 0
				py = -bullet_speed
			end
			table.insert(bullets,{x=player.x, y=player.y, w=1, h=1, px=px, py=py})
			setFireSFX("/sound/kaihuo2.mp3")
		end
	end
end

-- 获取窗口宽度
function getWindowWidth()
	width = love.graphics.getWidth()
	return width
end

-- 获取窗口高度
function getWindowHeight()
	height = love.graphics.getHeight()
	return height
end

-- 设置子弹状态
function setBulletsState()
	bullets = {}
end

-- 设置子弹开火和敌机交互
function setBulletsFire(dt)
	for i=#bullets, 1, -1 do
		v = bullets[i]
		v.x = v.x + v.px*dt
		v.y = v.y + v.py*dt
		local windowWidth = getWindowWidth()
		local windowHeight = getWindowHeight()
		if v.x > windowWidth or v.x < 0
		or v.y > windowHeight or v.y < 0 then
			table.remove(bullets,i)
		else
			for j=#enemys,1,-1 do
				if collision(v,enemys[j]) then
					table.remove(bullets,i)
					enemys[j].hp = enemys[j].hp - 1
					if enemys[j].hp <= 0 then
						table.remove(enemys,j)
						setEnemyDieSWF("/sound/nandie.mp3")
					end
				end
			end
		end
	end
end

--设置子弹贴图
function setPlayerBulletsPhoto(bulletsUrl)
	bulletsImage = love.graphics.newImage(bulletsUrl)
	width = bulletsImage:getWidth()
    height = bulletsImage:getHeight()
end

-- 绘制子弹
function setPlayerBullets()
	for _,v in ipairs(bullets) do
		love.graphics.rectangle("line",v.x, v.y-28, v.w, v.h)
	end
end

-- 绘制子弹贴图
function drawBulletsPhoto()
	for _,v in ipairs(bullets) do
		love.graphics.draw(bulletsImage, v.x, v.y, 0, 1, 1, width/23.5, height/11)
	end
end

-- 设置玩家死亡音效
function setPlayDieSWF(bgmUrl)
	bgm = love.audio.newSource(bgmUrl,"static")
	love.audio.play(bgm)
end

-- 设置敌机开火音效
function setEnemysFireSWF(bgmUrl)
	bgm = love.audio.newSource(bgmUrl,"static")
	love.audio.play(bgm)
end

-- 设置敌机死亡音效
function setEnemyDieSWF(bgmUrl)
	bgm = love.audio.newSource(bgmUrl,"static")
	love.audio.play(bgm)
end

-- 设置背景音乐
function setBGM(bgmUrl)
	bgm = love.audio.newSource(bgmUrl,"stream")
	bgm:setLooping(true)
	bgm:play()	
end

-- 设置开火音效
function setFireSFX(fireUrl)
	sfx = love.audio.newSource(fireUrl, "static")
	love.audio.play(sfx)
end

-- 设置角色数据
function setPlayerData(x,y,w,h)
	player = {x=x, y=y, w=w, h=h}
	return player
end

-- 设置游戏标题
function setGameTitle(gameTitle)
	love.window.setTitle(gameTitle)
end

-- 设置初始位置
function setDefaultPosition(x,y)
		vx = x
		vy = y
end

-- 设置场景贴图
function setBackgroudImg(imageUrl)
	backgroudImage = love.graphics.newImage(imageUrl)
	backgroudImageWidth = backgroudImage:getWidth()
    backgroudImageHeight = backgroudImage:getHeight()
end

-- 获取场景贴图
function getBackgroudImg()
	love.graphics.draw(backgroudImage, 60, 0, 0, 0.8, 0.8, backgroudImageHeight/2)
end

-- 设置角色贴图
function setPlayImg(imageUrl)
	playerImage = love.graphics.newImage(imageUrl)
	playerImageWidth = playerImage:getWidth()
    playerImageHeight = playerImage:getHeight()
end

-- 设置敌机贴图
function setEnemysImg(imageUrl)
	enemysImage = love.graphics.newImage(imageUrl)
	enemysImageWidth = enemysImage:getWidth()
    enemysImageHeight = enemysImage:getHeight()
end

-- 设置敌机子弹
function setEnemysBulletsImg(imageUrl)
	enemysBulletsImage = love.graphics.newImage(imageUrl)
	enemysBulletsWidth = enemysBulletsImage:getWidth()
  	enemysBulletsHeight = enemysBulletsImage:getHeight()
end

-- 绘制敌机贴图
function drawEnemysImg()
	for _,v in ipairs(enemys) do
		love.graphics.draw(enemysImage, v.x, v.y, 0, 0.5, 0.5, enemysImageWidth/6, enemysImageHeight/7)
	end
end

-- 绘制敌人子弹
function drawEnemysBulletsImg()
	for _,v in ipairs(enemysBullets) do
		love.graphics.draw(enemysBulletsImage, v.x, v.y, 1.6, 0.3, 0.5, enemysImageWidth, enemysImageHeight/4)
	end
end

-- 获取角色贴图
function getPlayerImgAndDefaultPosition()
	love.graphics.draw(playerImage, vx, vy, 0, 0.5, 0.5, playerImageWidth/2, playerImageHeight/4)
end

-- 获取角色骨架
function getPlayerSkeleton(type,x,y,w,h)
	love.graphics.setColor(255, 255, 255, 1)
	love.graphics.rectangle(type, x, y, w, h)
end